package org.example.workbench.controller;

import org.example.commons.constant.Constant;
import org.example.commons.domain.ErrorInfo;
import org.example.commons.utils.DateFormatUtils;
import org.example.commons.utils.UUIDUtils;
import org.example.settings.domain.User;
import org.example.workbench.domain.ActivityRemark;
import org.example.workbench.service.ActivityRemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;

@Controller
@RequestMapping("/activityRemker")
public class activityRemkerController {
    @Autowired
    private ActivityRemarkService activityRemarkService;

    @RequestMapping("/updateRemarkById")
    @ResponseBody
    public Object updateRemarkById(ActivityRemark activityRemark,HttpSession session){
        User user = (User)session.getAttribute(Constant.SESSION_USER);
        activityRemark.setEditFlag(Constant.EDIT_FLAG_YES);
        activityRemark.setEditBy(user.getId());
        activityRemark.setEditTime(DateFormatUtils.formatDateTime(new Date()));
        ErrorInfo errorInfo = new ErrorInfo();
        try {
            int i = activityRemarkService.updateRemarkById(activityRemark);
            if (i>0){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
            }else {
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("系统繁忙,请稍后.....");
            }
        }catch (Exception e){
            e.printStackTrace();
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙,请稍后.....");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("activityRemark",activityRemark);
        map.put("errorInfo",errorInfo);
        return  map;
    }
    @RequestMapping("/deleteRemarkById")
    @ResponseBody
    public Object deleteRemarkById(String id){
        ErrorInfo errorInfo = new ErrorInfo();
        try {
            int i = activityRemarkService.deleteRemarkById(id);
            if (i > 0) {
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
            } else {
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("系统繁忙，请稍后....");
            }
        }catch (Exception e){
            e.printStackTrace();
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙，请稍后....");
        }
        return errorInfo;
    }
    @RequestMapping("/insertOneRemker")
    @ResponseBody
    public Object insertOneRemker(ActivityRemark remark, HttpSession session){
        User user = (User)session.getAttribute(Constant.SESSION_USER);
        remark.setId(UUIDUtils.getUUID());
        remark.setCreateBy(user.getId());
        remark.setCreateTime(DateFormatUtils.formatDateTime(new Date()));
        remark.setEditFlag(Constant.EDIT_FLAG_NO);
        ErrorInfo errorInfo = new ErrorInfo();
        try {
            int i = activityRemarkService.insertOneRemark(remark);
            if(i>0){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
            }else {
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("系统繁忙，请稍后....");
            }
        }catch (Exception e){
            e.printStackTrace();
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙，请稍后....");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("errorInfo",errorInfo);
        map.put("remark",remark);

        return map;
    }
}
